import Qust from "/js/Qust.js";
import { $, $$ } from "/js/JSelect.js";
import { utils } from "/js/Utils.js";
const qust = Qust.create({
	baseUrl: "http://www.rushmc.top/api/",
});

/* 页面加载完 */
if (localStorage.getItem("uid") == undefined) {
	$$.navigateTo("/html/login.html");
}
if (utils.getQueryVariable("uid") != undefined) {
	window.mode = "others";
	$("#title").setText(`${utils.getQueryVariable("uname")}的个人空间`);
	$("#news-area").setAttr("class", "col-sm-6");
	$("#settings-area").setStyle("display", "none");
	getNews(1, utils.getQueryVariable("uid"));
} else {
	window.mode = "self";
	$("#user-avatar").setAttr("src", `http://www.rushmc.top/${localStorage.getItem("uavatar")}`);
	getNews(1);
}

/* 事件绑定 */
$("#page-home").click(() => {
	if (window.mode == "self") {
		getNews(1);
	} else {
		getNews(1, utils.getQueryVariable("uid"));
	}
});
$("#page-backwards").click(() => {
	if (window.currentPage == 1) {
		return false;
	} else {
		if (window.mode == "self") {
			getNews(window.currentPage - 1);
		} else {
			getNews(window.currentPage - 1, utils.getQueryVariable("uid"));
		}
	}
});
$("#page-forwards").click(() => {
	if (window.currentPage == window.maxPage) {
		return false;
	} else {
		if (window.mode == "self") {
			getNews(window.currentPage + 1);
		} else {
			getNews(window.currentPage + 1, utils.getQueryVariable("uid"));
		}
	}
});
$("#upload-pic").change((e) => {
	let file = e.target.files[0];
	qust.request({
		url: "updateAvatar",
		data: {
			avatar: file,
			id: localStorage.getItem("uid"),
		},
	}).then((res) => {
		location.reload();
	});
});
$("#submit-pwd-change").click(() => {
	if ($("#old-pwd").value != localStorage.getItem("upwd")) {
		$$.showModal({
			title: "输入错误",
			content: `旧密码输入错误`,
		});
		return false;
	}
	if ($("#new-pwd").value != $("#new-pwd2").value) {
		$$.showModal({
			title: "输入错误",
			content: `请核验你的新密码，两次输入不一致！`,
		});
		return false;
	}
	qust.request({
		url: "passwordUpdate",
		data: {
			id: localStorage.getItem("uid"),
			newPassword: $("#new-pwd").value,
		},
	}).then((res) => {
		if (res.code == 200) {
			$$.showModal({
				title: "修改成功",
				content: `${res.data.msg}`,
			});
		}
	});
});

/* 函数定义 */
function getNews(currentPage, uid = localStorage.getItem("uid")) {
	qust.request({
		url: `dynamicState/select/${uid}/${currentPage}/10`,
		method: "GET",
	}).then((res) => {
		if (res.code == 11000) {
			return false;
		}

		let news = res.data.data.records;
		let newCardsTemplate = ``;
		for (let key in news) {
			let curNew = news[key];

			let picsTemplate = ``;
			curNew.urls.forEach((item) => {
				picsTemplate += `<img src="http://www.rushmc.top/${item}" />`;
			});

			let delTemplate = window.mode == "others" ? `` : `<a class="del" href="#" onclick="window.del(${curNew.id})">删除动态</a>`;

			newCardsTemplate += `<div class="new-card">
            <div class="new-title" id="new-title">
			<a class="author" style="font-weight:500;font-size:16px" href="/html/user_center.html?uid=${curNew.userId}&uname=${curNew.username}">${curNew.username}</a><p>  在${curNew.creationTime.substr(
				0,
				10
			)}说</p>
                <div>${curNew.title}</div>
            </div>
            <div class="content">${curNew.content}</div>
            <div class="pics">
                ${picsTemplate}
            </div>
			${delTemplate}
        </div>`;
		}
		window.currentPage = res.data.data.current;
		window.maxPage = res.data.data.pages;
		$("#news").setHtml(newCardsTemplate);
		$("#news-controller-state").setText(`${res.data.data.current}/${res.data.data.pages}`);
	});
}

window.del = (id) => {
	if (confirm("确定删除吗?")) {
		qust.request({
			url: "dynamicState/delete",
			data: {
				id,
			},
		}).then((res) => {
			if (res.code == 200) {
				$$.showModal({
					title: "成功",
					content: res.data.msg,
				});
			}
			getNews(1);
		});
	}
};
